package com.jeesite.modules.common;

import com.qiniu.common.Config;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class QiniuUtils {
	
	private static final Logger logger = LoggerFactory.getLogger(QiniuUtils.class);
//
//	private static final String ACCESS_KEY="IDWiWjv6lgmkRfkennYMCeKobwLmp4Ay4DN1oarx"; //Config.getValue("qiniuAccessKey");
//	private static final String SECRET_KEY="3JwMM9SM2CncDjB84s9h6HLL-CvbN5KWCiXN4M45";//Config.getValue("qiniuSecretKey");
////	private static final String BUCKET="advertisement";//Config.getValue("qiniuBucket");
//	private static final Auth auth = Auth.create(ACCESS_KEY,SECRET_KEY);
//	private static final String BUCKET="lanstch";//Config.getValue("qiniuBucket");
//
//	private static final UploadManager uploadManager = new UploadManager();
////    private static final String domain = "http://o928reoe7.bkt.clouddn.com/";
//    private static final String domain = "https://image.lanstech.cn/";
//	private static final String yasuo="?imageView2/0/format/jpg/q/75|imageslim";

	// private static final String ACCESS_KEY="_3om_UZ25qI5kvsIRG53UAmJMtutNt_6UpN0M6Wz"; //Config.getValue("qiniuAccessKey");
// private static final String SECRET_KEY="_5Jsk6UsqHswnUR62NuyQnfx2b0upzli7eYjN576";//Config.getValue("qiniuSecretKey");
	private static final String SECRET_KEY="IDWiWjv6lgmkRfkennYMCeKobwLmp4Ay4DN1oarx";
	private static final String ACCESS_KEY="3JwMM9SM2CncDjB84s9h6HLL-CvbN5KWCiXN4M45";
	// private static final String BUCKET="advertisement";//Config.getValue("qiniuBucket");
	private static final String BUCKET="lanstch";//Config.getValue("qiniuBucket");
	private static final Auth auth = Auth.create(ACCESS_KEY,SECRET_KEY);
	private static final UploadManager uploadManager = new UploadManager();
	//    private static final String domain = "http://o928reoe7.bkt.clouddn.com/";
	private static final String domain = "https://image.lanstech.cn/";

	private static final String yasuo="?imageView2/0/format/jpg/q/75|imageslim";


	/**
	 * 获取上传凭证key
	 * @return 返回凭证key
	 */
	public static String generateSimpleUploadTicket() {
		try {
			//return auth.uploadToken(BUCKET, null, 36000, new StringMap().put("insertOnly", "1"));
			return auth.uploadToken(BUCKET);
		} catch (Exception e) {
			QiniuUtils.logger.error("获取七牛上传凭证异常：" + e.getMessage(), e);
		}
		return null;
	}
	
	 private static String getUpToken(){//return auth.uploadToken(BUCKET);
	     return auth.uploadToken(BUCKET, null, 36000, null);
	 }

	 public void upload(String path, String fileName) throws QiniuException{
		 //String[] temp=path.sp
		 Response res = uploadManager.put(path, fileName, getUpToken());
		 
	 }

    public static String upload(File file, String fileName) throws QiniuException{
        //String[] temp=path.sp
        // 设置一下上传的区域,不使用默认区域
        Config.zone = Zone.zone1();

        Response res = uploadManager.put(file, fileName, getUpToken());
        return domain + fileName;
    }

    public static String uploadBytes(byte[] data, String fileName) throws QiniuException {
        Config.zone = Zone.zone1();

        Response res = uploadManager.put(data, fileName, getUpToken());

        return domain + fileName;
    }

//    public static String getHtmlData(String fileName) throws IOException {
//
//        return HttpClientUtils.bodyPost(domain + fileName + ".html", "");
//
//    }
	
	public static String getToken() {   
	    /*Mac mac = new Mac(ACCESS_KEY, SECRET_KEY);  
	    PutPolicy putPolicy = new PutPolicy(BUCKET);  
	    try {  
	        String uptoken = putPolicy.token(mac);  
	        return uptoken;  
	    } catch (Exception e) {  */
	    return null;  
	//}
	}

/*	*//**
	 * 获取下载凭证
	 * @param key 文件key
	 * @param treatMethod 处理管道
	 * @return 下载凭证
	 *//*
	public static String generateDownloadTicket(String key, String treatMethod) {
		//七牛api地址：http://developer.qiniu.com/docs/v6/sdk/java-sdk.html#private-download
		try {
			Auth auth = Auth.create(HmsFileUtils.getSysContentItem(ContentUtils.QINIU_ACCESS_KEY),
					HmsFileUtils.getSysContentItem(ContentUtils.QINIU_SECRET_KEY));
			return auth.privateDownloadUrl(HmsFileUtils.getSysContentItem(ContentUtils.QINIU_DOWNLOAD_ADDRESS) + key
					+ treatMethod, Long.parseLong(HmsFileUtils.getSysContentItem(ContentUtils.QINIU_INVALIDATION_TIME)));
		} catch (Exception e) {
			QiniuUtils.logger.error("获取七牛下载凭证异常：" + e.getMessage(), e);
		}
		return null;
	}*/
}
